/*****************************************************************************//*!
* file  : systask.c
* creat : apleilx
* note :  除初始化外，禁止该任务及其子程序使用含有阻塞的功能，包括数据存储，USB操作等等
* data  : 2019/07/02
*******************************************************************************/
#define _SYSTASK_MODULE_
#include "os_obj.h"
#include "pincfg.h"
#include "systask.h"
#include "nor_cmd.h"
#include "bsp_spi_nor.h"
#include "stdio.h"
#include "bsp.h"
#include "sys_com.h"
#include "string.h"
#include "stdlib.h"
#include "common.h"
#include "sys_malloc.h"

void sys_init(void);
void eep_iic_init(void);
void dma_test(void);

/*****************************************************************************//*!
* @brief   storage task.
* @param   none
* @return  none
* @ Pass/ Fail criteria: none
*****************************************************************************/
void SysTask(void const *argument)
{  
    thread_create();
    
    dbg_print("system start\n");
    dbg_print("HCLK = %d\n", CLK_GetHCLKFreq());
    dbg_print("PCLK0 = %d\n", CLK_GetPCLK0Freq());
    dbg_print("PCLK1= %d\n", CLK_GetPCLK1Freq());
    dbg_print("CPU CLK= %d\n", CLK_GetCPUFreq());
    
    pulse_init(&sys_st.pulse, sys_st.ticks);
    
	sys_st.buff = mem_ccm.api->malloc(126);
	
	memset(sys_st.buff, 0, 126);
	
    for(;;){
        
        osDelay(ms_ticks(4));
        
		dma_test();
		
        pulse_create(&sys_st.pulse, sys_st.ticks);
		
        if(sys_st.pulse.bPulse_100ms)
        {
            uint8_t buff[25];
			
            TimeRun(&sys_st.sw_rtc);
			
            sys_st.sig_rtc = time_to_sig(&sys_st.sw_rtc);
			
            TimeToAscii(&sys_st.sw_rtc,buff);
        }
		
		if(sys_st.task_info)
		{
			sys_st.task_info = 0;
			os_task_info();
		}
		
		sys_st.info.stack_used = stack_used();
		
		if(SPIM->CTL1 & SPIM_CTL1_CCMEN_Msk)
		{
			sys_st.info.ccm_used = ccm_used();
		}
		
		if((sys_st.pulse.AutoCnt & 0xF) < 1)
		{
			o_led2.en();
		}
		else
		{
			o_led2.dis();
		}
		
		if(sys_st.pulse.bPulse_1s)
		{
			o_led1.tog();
		}
    }
}

/*****************************************************************************
* @brief   sys init.
* @param   none
* @return  none
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sys_init(void)
{
  
}

uint32_t rt_tick_get(void)
{
    return sys_st.ticks;
}

